package com.zmcn.flowindex.business.sys.service.impl;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import net.sf.json.JSONArray;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.zmcn.flowindex.bean.po.LoginLog;
import com.zmcn.flowindex.bean.po.Role;
import com.zmcn.flowindex.bean.vo.LoginLogVO;
import com.zmcn.flowindex.bean.vo.MenuVO;
import com.zmcn.flowindex.bean.vo.UserVO;
import com.zmcn.flowindex.business.sys.dao.SysDao;
import com.zmcn.flowindex.business.sys.service.SysService;
import com.zmcn.flowindex.business.sys.util.SysUtil;
import com.zmcn.flowindex.fw.action.Page;

import java.util.Collections;

/**
 * 
 * 后台系统模块接口
 * 
 * @author <b>欧建东<br/>
 *         Email:oyjd@163.com</b>
 * @version 1.0
 */
@Component
@Transactional(readOnly = true)
public class SysServiceImpl implements SysService {
	private SysDao dao;

	@Autowired
	public void setDao(SysDao dao) {
		this.dao = dao;
	}

	/**
	 * 初始化系统常量参数
	 */
	@SuppressWarnings("unchecked")
	public void init() {
		SysUtil.roleToLinks = dao.getLinks();
		SysUtil.roleToModuleOp = dao.getModuleOp();
		SysUtil.roleToFlatMenu.clear();
		SysUtil.roleToTreeMenu.clear();
		SysUtil.menus.clear();
		SysUtil.role.clear();

		// 设置roleId->roleName
		List<Role> roles = (List<Role>) dao.queryAll(Role.class);
		for (Role r : roles) {
			SysUtil.role.put(r.getRoleId(), r.getName());
		}

		// menuId->MenuVO
		List<MenuVO> allMenus = dao.getAllMenu();
		for (MenuVO menuVO : allMenus) {
			SysUtil.menus.put(menuVO.getMenuId(), menuVO);
		}

		// 菜单处理
		for (String roleId : SysUtil.roleToLinks.keySet()) {
			List<MenuVO> aimMenus = new ArrayList<MenuVO>();
			for (String link : SysUtil.roleToLinks.get(roleId)) {
				for (MenuVO vo : allMenus) {
					if (link.equals(vo.getLink())) {
						SysUtil.doParent(allMenus, aimMenus, vo.getMenuId());
					}
				}
			}
			// 排序菜单
			Collections.sort(aimMenus, new Comparator<MenuVO>() {
				public int compare(MenuVO o1, MenuVO o2) {
					return o1.getMenuId().compareTo(o2.getMenuId());
				}
			});

			SysUtil.roleToFlatMenu.put(roleId, aimMenus);

			//
			JSONArray m = SysUtil.getMenuLevelByPid(aimMenus, "");
			SysUtil.roleToTreeMenu.put(roleId, m);
		}

	}

	/**
	 * 根据帐号获取用户信息
	 * 
	 * @param account
	 * @return
	 */
	public UserVO getUser(String account) {
		return dao.getUser(account);
	}

	/**
	 * 记录登入日志
	 * 
	 * @param account
	 * @param custIp
	 * @param status
	 */
	@Transactional(readOnly = false)
	public void saveLoginLog(String account, String custIp, int status) {
		LoginLog log = new LoginLog();
		log.setAccount(account);
		log.setCustIp(custIp);
		log.setStatus(status);
		log.setLoginTime(new Date());
		dao.create(log);
	}

	/**
	 * 查询登入日志列表
	 * 
	 * @param page
	 * @return
	 */
	public Page<LoginLogVO> getLoginLog(Page<LoginLogVO> page) {
		return dao.getLoginLog(page);
	}
}
